﻿Public Class frmLoginUserManagement
    Dim dbBase As New dbUser
    Dim dbSys As New dbSystem
    Dim dsBase As DataSet
    Dim frmCtrl As New control
    Dim TypeAct As String
    Dim EmployerID As String
    Dim dtPermission As New DataTable
    Public EntryID As String

    Private Sub LoadData()
        txtSearchEmployer.Visible = False
        txtSearchEmployer.Initialize(New dbEmployer().GetEmployerList(), "EmployerID", lblEmployerLink)
        dsBase = dbBase.GetUserLoginDetail(EntryID)
        UcHeader1.Iniliaze("Management : Login User", Me, New frmLoginUserList, Nothing, "", False)
        Dim dtUserDetail As DataTable = dsBase.Tables(0)
        Dim dtPermission As DataTable = dsBase.Tables(1)
        Dim dtLog As DataTable = dsBase.Tables(2)
        If (dtUserDetail.Rows.Count > 0) Then
            txtGenID.Text = dtUserDetail.Rows(0)("GenID").ToString().Trim()
            txtLoginID.Text = dtUserDetail.Rows(0)("LoginID").ToString().Trim()
            Dim text As Array = dtUserDetail.Rows(0)("Employer").ToString().Trim().Split("-")
            lblEmployerLink.Text = text(1).ToString().Trim()
            EmployerID = text(0).ToString().Trim()
        End If
        Dim rowCount As Integer = dtPermission.Rows.Count
        If (rowCount > 0) Then
            dgPermission.Rows.Add(rowCount)
            For i = 0 To rowCount - 1
                Try
                    dgPermission.Rows(i).Cells(0).Value = dtPermission.Rows(i)(1).ToString().Trim()
                    dgPermission.Rows(i).Cells(1).Value = dtPermission.Rows(i)(0).ToString().Trim()
                    dgPermission.Rows(i).Cells(2).Value = dtPermission.Rows(i)(2).ToString().Trim()
                    dgPermission.Rows(i).Cells(3).Value = CBool(CInt(dtPermission.Rows(i)(3)))
                    dgPermission.Rows(i).Cells(4).Value = CBool(CInt(dtPermission.Rows(i)(4)))
                    dgPermission.Rows(i).Cells(5).Value = CBool(CInt(dtPermission.Rows(i)(5)))
                    dgPermission.Rows(i).Cells(6).Value = CBool(CInt(dtPermission.Rows(i)(6)))
                    dgPermission.Rows(i).Cells(7).Value = CBool(CInt(dtPermission.Rows(i)(7)))
                Catch ex As Exception
                End Try
            Next
        End If
        rowCount = dtLog.Rows.Count
        If (rowCount > 0) Then
            dgUserLog.Rows.Add(rowCount)
            For i = 0 To rowCount - 1
                Try
                    dgUserLog.Rows(i).Cells(0).Value = dtLog.Rows(i)(0).ToString().Trim()
                    dgUserLog.Rows(i).Cells(1).Value = dtLog.Rows(i)(1).ToString().Trim()
                Catch ex As Exception
                End Try
            Next
        End If
        If (EntryID <> "") Then
            TypeAct = "U"
            UcHeader1.ActionCtrl(True, False, False, True, True, True, False, False, False, True)
        Else
            TypeAct = "I"
            UcHeader1.ActionCtrl(False, False, False, False, False, True, False, False, False, True)
        End If
        dtPermission.Columns.Add("frmGenID")
        dtPermission.Columns.Add("perOpen")
        dtPermission.Columns.Add("perInsert")
        dtPermission.Columns.Add("perUpdate")
        dtPermission.Columns.Add("perDelete")
        dtPermission.Columns.Add("perPrint")
    End Sub

    Private Sub frmLoginUserManagement_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        frmCtrl.reDesign(Me)
        EmployerID = ""
        LoadData()
    End Sub

    Private Sub GetPermission()
        Dim rowCount As Integer = dgPermission.Rows.Count
        dtPermission.Rows.Clear()
        For i = 0 To rowCount - 1
            Try
                dtPermission.Rows.Add(dgPermission.Rows(i).Cells(0).Value, _
                                  Convert.ToInt16(dgPermission.Rows(i).Cells(3).Value), _
                                  Convert.ToInt16(dgPermission.Rows(i).Cells(4).Value), _
                                  Convert.ToInt16(dgPermission.Rows(i).Cells(5).Value), _
                                  Convert.ToInt16(dgPermission.Rows(i).Cells(6).Value), _
                                  Convert.ToInt16(dgPermission.Rows(i).Cells(7).Value))
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        Next
    End Sub

    ''================================== Control Header ==============================================''
    Private Sub tsNew_Click(ByVal sender As Object) Handles UcHeader1.tsNewClick
        Dim OpenForm As New frmLoginUserManagement
        OpenForm.EntryID = ""
        frmCtrl.OpenFormDetail(Me, OpenForm)
    End Sub

    Private Sub tsDelete_Click(ByVal sender As Object) Handles UcHeader1.tsDeleteClick
        If (EntryID.Trim() <> "") Then
            TypeAct = "D"
            Dim result As String = dbBase.LoginUserManagement(TypeAct, txtGenID.Text.Trim(), txtLoginID.Text.Trim(), txtLoginPassword.Text.Trim(), EmployerID, frmGlobal.LoginID)
            If (result.IndexOf("success") > -1) Then
                    frmCtrl.openChild(New frmLoginUserList, Me.ParentForm)
            Else : MessageBox.Show(result)
            End If
        Else : MessageBox.Show("Login User is not Exist")
        End If
    End Sub

    Private Sub tsSave_Click(ByVal sender As Object) Handles UcHeader1.tsSaveClick
        Dim status As Boolean = True
        If (TypeAct = "I" And txtLoginPassword.Text.Trim() = "") Then
            MessageBox.Show("Password is Empty")
            status = False
        End If
        If (txtLoginID.Text.Trim() <> "" And status = True And EmployerID <> "") Then
            Dim result As Array = dbBase.LoginUserManagement(TypeAct, txtGenID.Text.Trim(), txtLoginID.Text.Trim(), txtLoginPassword.Text.Trim(), EmployerID, frmGlobal.LoginID).Split("-")
            If (result(0).IndexOf("success") > -1) Then
                txtGenID.Text = result(1)
                Dim dtResult As New DataTable
                dtResult.Columns.Add("frmGenID")
                dtResult.Columns.Add("perOpen")
                dtResult.Columns.Add("perInsert")
                dtResult.Columns.Add("perUpdate")
                dtResult.Columns.Add("perDelete")
                dtResult.Columns.Add("perPrint")
                Dim rowCount As Integer = dgPermission.Rows.Count
                For i = 0 To rowCount - 1
                    Try
                        dtResult.Rows.Add(dgPermission.Rows(i).Cells(0).Value, _
                                          Convert.ToInt16(dgPermission.Rows(i).Cells(3).Value), _
                                          Convert.ToInt16(dgPermission.Rows(i).Cells(4).Value), _
                                          Convert.ToInt16(dgPermission.Rows(i).Cells(5).Value), _
                                          Convert.ToInt16(dgPermission.Rows(i).Cells(6).Value), _
                                          Convert.ToInt16(dgPermission.Rows(i).Cells(7).Value))
                    Catch ex As Exception
                        MessageBox.Show(ex.Message)
                    End Try
                Next
                Dim rs As Boolean = dbBase.SetUserPermission(txtGenID.Text, dtResult, frmGlobal.LoginID)
                If (rs) Then
                    frmCtrl.openChild(New frmLoginUserList, Me.ParentForm)
                Else : MessageBox.Show("Error in Save Progress")
                End If
            Else : MessageBox.Show(result(0))
            End If
        Else : MessageBox.Show("Please Fill All Field")
        End If
    End Sub

    Private Sub tsPrint_Click(ByVal sender As Object) Handles UcHeader1.tsPrintClick
        MessageBox.Show("Not included in prototype")
    End Sub
    ''================================== Control Header [End] ==============================================''

    Private Sub txtLoginPassword_TextChanged(sender As Object, e As EventArgs) Handles txtLoginPassword.TextChanged
        If (txtLoginPassword.Text.Trim().Length() > 0) Then
            If (TypeAct = "U") Then
                lblChangePass.Visible = True
            End If
        Else : lblChangePass.Visible = False
        End If
    End Sub

    Private Sub btnSearchEmployer_Click(sender As Object, e As EventArgs) Handles btnSearchEmployer.Click
        txtSearchEmployer._Show()
    End Sub

    Private Sub lblEmployerLink_TextChanged(sender As Object, e As EventArgs) Handles lblEmployerLink.TextChanged
        txtSearchEmployer.Visible = False
        If lblEmployerLink.Text = "load" Then
            lblEmployerLink.Text = txtSearchEmployer.dsResult.Tables(0).Rows(0)("Employer FullName").ToString()
            EmployerID = txtSearchEmployer.dsResult.Tables(0).Rows(0)("EmployerID").ToString()
        End If
    End Sub
End Class